package com.webwork.mood.record.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.record.dao.AwardRateDao;
import com.webwork.mood.record.dataobject.AwardInfo;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;
import com.webwork.mood.util.StrManager;

@Repository
public class AwardRateDaoImpl extends SimpleDao implements AwardRateDao {

	public AwardRateDaoImpl() {
		super();
		this.dbTable = "t_award_rate";
		dbColumns.clear();
		dbColumns.put("awardName", "AWARD_NAME");
		dbColumns.put("count", "COUNT");
		dbColumns.put("rate", "RATE");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof AwardInfo))
			return new ColumnLimit();
		AwardInfo o = (AwardInfo) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getAwardName())) {
			ret.add(dbColumns.get("awardName") + " = ");
		}
		if (o.getCount() != 0) {
			ret.add(dbColumns.get("count") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		AwardInfo o = (AwardInfo) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getAwardName())) {
			ret.add(o.getAwardName());
		}
		if (o.getCount() != 0) {
			ret.add(o.getCount());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		AwardInfo o = (AwardInfo) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getAwardName());
		objs.add(o.getCount());
		objs.add(o.getRate());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		AwardInfo ret = new AwardInfo();
		ret.setAwardName((String) map.get(dbColumns.get("awardName")));
		ret.setCount((Integer) map.get(dbColumns.get("count")));
		ret.setRate((Float) map.get(dbColumns.get("rate")));
		return ret;
	}
	
	@Override
	public void insert(AwardInfo o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(AwardInfo o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("awardName") + " = ");
		limit.add(dbColumns.get("count") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getAwardName());
		objs.add(o.getCount());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String awardName, int count) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("awardName") + " = ");
		limit.add(dbColumns.get("count") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, awardName, count);
	}

	@Override
	public List<AwardInfo> query(AwardInfo o, int page, int size) {
		List<AwardInfo> ret = new ArrayList<AwardInfo>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((AwardInfo) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(AwardInfo o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}

}
